#include "SetWidget.h"
#include "ui_SetWidget.h"
#include <QScrollBar>
#include <QRegularExpressionValidator>
#include "Assemblies/Base/AppData.h"

SetWidget::SetWidget(QWidget *parent) :
    QWidget(parent), ui(new Ui::SetWidget)
{
    ui->setupUi(this);

    Init();
    OnConnec();
}

SetWidget::~SetWidget() {
    delete ui;
}

void SetWidget::Init() const
{
    /* 隐藏水平滚动条 */
    ui->scrollArea->horizontalScrollBar()->setHidden(true);

    /* 设置IP输入限制*/
    ui->lineEdit_ip->setInputMask("000.000.000.000; ");
    ui->lineEdit_ip->setText(AppData::Instance()->GetLocalIp());

    ui->lineEdit_remote->setInputMask("000.000.000.000; ");
    ui->lineEdit_remote->setText(AppData::Instance()->GetRemoteIp());

    // const auto *validator = new QRegularExpressionValidator(
    //     QRegularExpression("^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$"), ui->lineEdit_ip);
    //
    // ui->lineEdit_ip->setValidator(validator);

    ui->lineEdit_port->setInputMask("00000; ");
    ui->lineEdit_port->setText(QString::number(AppData::Instance()->GetPort()));
    // validator = new QRegularExpressionValidator(QRegularExpression("^\\d{1,5}$"), ui->lineEdit_port);
    // ui->lineEdit_port->setValidator(validator);

}

void SetWidget::OnConnec()
{
    connect(ui->pushButton, &QPushButton::clicked, this, &SetWidget::OnSave);
}

void SetWidget::OnSave() const
{
    AppData::Instance()->SetLocalIp(ui->lineEdit_ip->text());
    AppData::Instance()->SetRemoteIp(ui->lineEdit_remote->text());
    AppData::Instance()->SetPort(ui->lineEdit_port->text().toInt());
}
